# Maintainer: Bruno Pagani <archange@archlinux.org>
# Contributor: Ronald van Haren <ronald.archlinux.org>
# Contributor: damir <damir@archlinux.org>

_pkg=netcdf-fortran
_mpi=openmpi
pkgname=${_pkg}-${_mpi}
pkgver=4.6.0
pkgrel=1
pkgdesc="NetCDF fortran bindings with parallel support (${_mpi} version)"
arch=(loong64 x86_64)
url="https://github.com/Unidata/netcdf-fortran"
license=(custom)
depends=(gcc-libs netcdf-openmpi curl)
makedepends=(cmake gcc-fortran doxygen)
checkdepends=(valgrind inetutils)
provides=("${_pkg}")
conflicts=("${_pkg}")
options=(!makeflags staticlibs)
source=(${url}/archive/v${pkgver}/${_pkg}-${pkgver}.tar.gz)
sha256sums=('8194aa70e400c0adfc456127c1d97af2c6489207171d13b10cd754a16da8b0ca')

build() {
  export CC=mpicc FC=mpifort
  cmake -B build -S ${_pkg}-${pkgver} \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_BUILD_TYPE=Release \
    -DENABLE_FILTER_TEST=ON \
    -DENABLE_LARGE_FILE_TESTS=ON \
    -DENABLE_PARALLEL_TESTS=ON \
    -DTEST_WITH_VALGRIND=ON
  make -C build
}

check() {
  export OMPI_MCA_opal_warn_on_missing_libcuda=0
  # This is required starting with OpenMPI 3.0 when trying to run more
  # processes than the number of available cores
  export OMPI_MCA_rmaps_base_oversubscribe=yes

  make -C build test
}

package() {
  make -C build DESTDIR="${pkgdir}" install
  install -Dm644 ${_pkg}-${pkgver}/COPYRIGHT -t "${pkgdir}"/usr/share/licenses/${_pkg}/
  # Remove unwanted artifacts https://github.com/Unidata/netcdf-fortran/issues/338
  rmdir "${pkgdir}"/usr/include/CMakeFiles/{netcdff{,_c}.dir,}
  rm "${pkgdir}"/usr/lib/objects-RelWithDebInfo/{nf_,f}test_c/f03lib.c.o
  rmdir "${pkgdir}"/usr/lib/objects-RelWithDebInfo/{{nf_,f}test_c,}
  rm "${pkgdir}"/usr/lib/objects-Release/netcdff_c/nf_{lib,v2compat}.c.o
  rmdir "${pkgdir}"/usr/lib/objects-Release/{netcdff_c,}
}
